package com.ist.word.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.Timer;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.ist.word.audio.AudioPlayer;
import com.ist.word.datasoure.SQLiteDataSource;
import com.ist.word.logic.GameConfigurations;

public class GameSplashScreen extends JFrame implements ActionListener {

	private JProgressBar timeJProgressBar;
	Image backgroundImage = Toolkit.getDefaultToolkit().getImage(
			this.getClass().getResource(GameConfigurations.GAMESPLASHSCEEN));
	private JPanel Gamesplashpanel;
	private Timer GameSplashTimer;
	private int gameSplashScreenStatus=0;
	private AudioPlayer audioPlayer;

	static private final Dimension mainFrameSize = new Dimension(
			GameConfigurations.GAMESPLASH_SCRENNWIDTH,
			GameConfigurations.GAMESPLASH_SCRENNHEIGHT);
	private final JComponent backgroundCanvas = new JComponent() {
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics gg = g.create();
			gg.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
			gg.dispose();
		}
	};

	public Dimension getPreferredSize() {
		return mainFrameSize;
	}

	public Dimension getMaximumSize() {
		return mainFrameSize;
	}

	public Dimension getMinimumSize() {
		return mainFrameSize;
	}

	public Dimension getSize() {
		return mainFrameSize;
	}

	private void doLay() {
		Container container = getContentPane();
		container.add(backgroundCanvas);
		container.setSize(getWidth(), getHeight());
		pack();
		setVisible(true);
		audioPlayer=new AudioPlayer(GameConfigurations.GAMESPLASHMUSIC_LOADING);
		audioPlayer.play();
	}

	void init() {
		GameSplashTimer = new Timer(1000, this);
		timeJProgressBar = new JProgressBar();
		timeJProgressBar.setLocation(45, 374);
		timeJProgressBar.setSize(500, 20);
		timeJProgressBar.setBackground(new Color(109, 170, 251, 0));
		timeJProgressBar.setForeground(new Color(179, 229, 10, 70));
		timeJProgressBar.setBorder(null);
		timeJProgressBar.setMinimum(0);
		timeJProgressBar.setMaximum(5);
		timeJProgressBar.setStringPainted(true);
		timeJProgressBar.setValue(0);

		Gamesplashpanel = new JPanel();
		Gamesplashpanel.setLayout(null);
		Gamesplashpanel.setOpaque(false);
		Gamesplashpanel.add(timeJProgressBar);
		this.setTitle("Refactor");
		this.setIconImage(Toolkit.getDefaultToolkit().createImage(
				this.getClass().getResource(GameConfigurations.GAMETITLE_BARICON)));
		this.setLayout(new BorderLayout());
		this.add(Gamesplashpanel, BorderLayout.CENTER);
		this.setSize(mainFrameSize);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
		//initial the database file
		/*SQLiteDataSource linkDB = new SQLiteDataSource();
		try {
			linkDB.getConneection().close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	private void initState() {
		timeJProgressBar.setValue(0);
		GameSplashTimer.start();
	}

	/**
	 * @param args
	 * @return
	 */
	public GameSplashScreen() {
		super();
		init();
		initState();
		GameSplashTimer.start();
		doLay();

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GameSplashScreen();

	}
	
	 //1 finished, 0 loading
	public int getStatus() {
		if (timeJProgressBar.getValue()>=4) {
			timeJProgressBar.setValue(0);
			return 1;
		}else {
			return 0;
		}
	}
	public int getGameSplashScreenStatus() {
		return gameSplashScreenStatus;
	}

	public void setGameSplashScreenStatus(int gameSplashScreenStatus) {
		this.gameSplashScreenStatus = gameSplashScreenStatus;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (GameSplashTimer == e.getSource()) {
			int value = timeJProgressBar.getValue();
			if (value <= 4){
				timeJProgressBar.setValue(++value);
				}
			else {
				gameSplashScreenStatus=1;
				GameSplashTimer.stop();
			}
		//	System.out.println(gameSplashScreenStatus);
		}
	}
}
